package com.example.a99544.uidemo.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a99544.uidemo.R;
import com.example.a99544.uidemo.activity.MainActivity;
import com.example.a99544.uidemo.activity.SelectSpotActivity;
import com.example.a99544.uidemo.activity.SpotActivity;
import com.example.a99544.uidemo.model.City;

import java.util.List;

public class CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder>{

    private List<City> mCityList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View cityView;
        ImageView cityImage;
        TextView cityName;
        Button button;

        public ViewHolder(View view) {
            super(view);
            cityView = view;
            cityImage = (ImageView) view.findViewById(R.id.city_image);
            cityName = (TextView) view.findViewById(R.id.city_name);
        }
    }

    public CityAdapter(List<City> cityList) {
        mCityList = cityList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.city_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.cityView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(v.getContext(), SelectSpotActivity.class);

                v.getContext().startActivity(intent);
                int position = holder.getAdapterPosition();
                intent.putExtra("city",mCityList.get(position).getName());
                City city = mCityList.get(position);
                //Toast.makeText(v.getContext(), "you clicked view " + city.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        /*holder.cityImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                City city = mCityList.get(position);
                Toast.makeText(v.getContext(), "you clicked image " + city.getName(), Toast.LENGTH_SHORT).show();
            }
        });*/
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        City city = mCityList.get(position);
        holder.cityImage.setImageResource(city.getImageId());
        holder.cityName.setText(city.getName());
    }

    @Override
    public int getItemCount() {
        return mCityList.size();
    }

}